package com.ptnb.plugin.junkcode.task

import com.ptnb.plugin.junkcode.ext.JunkCodeConfig
import com.ptnb.plugin.junkcode.template.ManifestTemplate
import com.ptnb.plugin.junkcode.template.ResTemplate
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.TypeSpec
import groovy.text.GStringTemplateEngine
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.OutputDirectories
import org.gradle.api.tasks.TaskAction
import org.gradle.internal.impldep.org.apache.commons.lang.RandomStringUtils

import javax.lang.model.element.Modifier

class AndroidJunkCodeTask extends DefaultTask {

    static def random = new Random()

    static abc = "abcdefghijklmnopqrstuvwxyz".toCharArray()

    @Nested
    JunkCodeConfig config = new JunkCodeConfig()

    @Input
    String manifestPackageName = ""

    @OutputDirectories
    File outDir

    @TaskAction
    void generateJunkCode() {
        if (outDir.exists()) {
            outDir.deleteDir()
        }
        //通过成类
        generateClasses()
        //生成资源
        generateRes()
    }

    /**
     * 生成java代码和AndroidManifest.xml
     */
    void generateClasses() {
        def javaDir = new File(outDir, "java")
        for (int i = 0; i < config.packageCount; i++) {
            String packageName = config.packageBase + "." + generateName(i)
            //生成Activity
            for (int j = 0; j < config.activityCountPerPackage; j++) {
                def activityPreName = generateName(j)
                generateActivity(packageName, activityPreName)
            }
            //生成其它类
            for (int j = 0; j < config.otherCountPerPackage; j++) {
                def className = generateName(j).capitalize()
                def typeBuilder = TypeSpec.classBuilder(className)
                typeBuilder.addModifiers(Modifier.PUBLIC)
                for (int k = 0; k < config.methodCountPerClass; k++) {
                    def methodName = generateName(k)
                    def methodBuilder = MethodSpec.methodBuilder(methodName)
                    generateMethods(methodBuilder)
                    typeBuilder.addMethod(methodBuilder.build())
                }
                def fileBuilder = JavaFile.builder(packageName, typeBuilder.build())
                fileBuilder.build().writeTo(javaDir)
            }
        }
    }

    /**
     * 生成随机方法
     * @param methodBuilder
     */
    static void generateMethods(MethodSpec.Builder methodBuilder) {
        switch (random.nextInt(6)) {
            case 0:
                methodBuilder.addCode(junkCodeTwo())
                break
            case 1:
                methodBuilder.addCode(""
                        + "int total = 0;\n"
                        + "for (int i = 0; i < 10; i++) {\n"
                        + "  total += i;\n"
                        + "}\n")
                break
            case 2:
                methodBuilder.beginControlFlow("try")
                        .addStatement("throw new Exception(\$S)", "Failed")
                        .nextControlFlow("catch (\$T e)", Exception.class)
                        .addStatement("throw new \$T(e)", RuntimeException.class)
                        .endControlFlow()
                break
            case 3:
                methodBuilder.returns(Date.class)
                        .addCode(junkCodeOne())
                        .addStatement("return new \$T()", Date.class)
                break
            case 4:
                methodBuilder.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                        .returns(void.class)
                        .addParameter(String[].class, "args")
                        .addStatement("\$T.out.println(\$S)", System.class, generateName(15))
                break
            default:
                methodBuilder.addCode(junkCodeOne())
        }
    }

    /**
     * 生成Activity
     * @param packageName
     * @param activityPreName
     */
    void generateActivity(String packageName, String activityPreName) {
        def javaDir = new File(outDir, "java")
        def className = activityPreName.capitalize() + "Activity"
        def layoutName = "${config.resPrefix.toLowerCase()}${packageName.replace(".", "_")}_activity_${activityPreName}"
        generateLayout(layoutName)
        if (!config.excludeActivityJavaFile) {
            def typeBuilder = TypeSpec.classBuilder(className)
            typeBuilder.superclass(ClassName.get("android.app", "Activity"))
            typeBuilder.addModifiers(Modifier.PUBLIC)
            //onCreate方法
            def bundleClassName = ClassName.get("android.os", "Bundle")
            typeBuilder.addMethod(MethodSpec.methodBuilder("onCreate")
                    .addAnnotation(Override.class)
                    .addModifiers(Modifier.PROTECTED)
                    .addParameter(bundleClassName, "savedInstanceState")
                    .addStatement("super.onCreate(savedInstanceState)")
                    .addCode(junkCodeOne())
                    .addStatement("setContentView(\$T.layout.${layoutName})", ClassName.get(manifestPackageName, "R"))
                    .build())
            //其它方法
            for (int j = 0; j < config.methodCountPerClass; j++) {
                def methodName = generateName(j)
                def methodBuilder = MethodSpec.methodBuilder(methodName)
                generateMethods(methodBuilder)
                typeBuilder.addMethod(methodBuilder.build())
            }
            def fileBuilder = JavaFile.builder(packageName, typeBuilder.build())
            fileBuilder.build().writeTo(javaDir)
        }
        addToManifestByFileIo(className, packageName)
    }

    /**
     * 生成资源文件
     */
    void generateRes() {
        //生成drawable
        for (int i = 0; i < config.drawableCount; i++) {
            def drawableName = "${config.resPrefix.toLowerCase()}${generateName(i)}"
            generateDrawable(drawableName)
        }
        //生成string
        for (int i = 0; i < config.stringCount; i++) {
            def name = "${config.resPrefix.toLowerCase()}${generateName(i)}"
            def value = name
            addStringByFileIo(name, value)
        }
    }

    /**
     * 生成layout
     * @param layoutName
     */
    void generateDrawable(String drawableName) {
        def drawableFile = new File(outDir, "res/drawable/${drawableName}.xml")
        if (!drawableFile.getParentFile().exists()) {
            drawableFile.getParentFile().mkdirs()
        }
        if (!drawableFile.exists()) {
            drawableFile.createNewFile()
        }
        FileWriter writer
        try {
            writer = new FileWriter(drawableFile)
            def template = ResTemplate.DRAWABLE
            writer.write(template.toString())
        } catch (Exception e) {
            e.printStackTrace()
        } finally {
            if (writer != null) {
                writer.close()
            }
        }
    }


    /**
     * 生成layout
     * @param layoutName
     */
    void generateLayout(String layoutName) {
        def layoutFile = new File(outDir, "res/layout/${layoutName}.xml")
        if (!layoutFile.getParentFile().exists()) {
            layoutFile.getParentFile().mkdirs()
        }
        if (!layoutFile.exists()) {
            layoutFile.createNewFile()
        }
        FileWriter writer
        try {
            writer = new FileWriter(layoutFile)
            def template = ResTemplate.LAYOUT_TEMPLATE
            writer.write(template.toString())
        } catch (Exception e) {
            e.printStackTrace()
        } finally {
            if (writer != null) {
                writer.close()
            }
        }
    }


    /**
     * 通过文件读写流的方式将新创建的Activity加入清单文件
     *
     * @param activityName
     * @param packageName
     */
    void addToManifestByFileIo(String activityName, String packageName) {
        def manifestFile = new File(outDir, "AndroidManifest.xml")
        if (!manifestFile.getParentFile().exists()) {
            manifestFile.getParentFile().mkdirs()
        }
        if (!manifestFile.exists()) {
            def template = ManifestTemplate.TEMPLATE
            FileWriter writer
            try {
                writer = new FileWriter(manifestFile)
                writer.write(template.toString())
            } catch (Exception e) {
                e.printStackTrace()
            } finally {
                if (writer != null) {
                    writer.close()
                }
            }
        }
        FileReader reader
        FileWriter writer
        try {
            reader = new FileReader(manifestFile)
            StringBuilder sb = new StringBuilder()
            // 每一行的内容
            String line = ""
            while ((line = reader.readLine()) != null) {
                // 找到application节点的末尾
                if (line.contains("</application>")) {
                    // 在application节点最后插入新创建的activity节点
                    def binding = [
                            packageName : packageName,
                            activityName: activityName,
                    ]
                    def template = makeTemplate(ManifestTemplate.ACTIVITY_NODE, binding)
                    sb.append(template.toString() + "\n")
                }
                sb.append(line + "\n")
            }
            String content = sb.toString()
            writer = new FileWriter(manifestFile)
            writer.write(content)
        } catch (Exception e) {
            e.printStackTrace()
        } finally {
            if (reader != null) {
                reader.close()
            }
            if (writer != null) {
                writer.close()
            }
        }
    }

    /**
     * 将string写入strings.xml
     * @param name
     * @param value
     */
    void addStringByFileIo(String name, String value) {
        //生成string
        def stringFile = new File(outDir, "res/values/strings.xml")
        if (!stringFile.getParentFile().exists()) {
            stringFile.getParentFile().mkdirs()
        }
        if (!stringFile.exists()) {
            stringFile.createNewFile()
            FileWriter writer
            try {
                writer = new FileWriter(stringFile)
                def template = ResTemplate.TEMPLATE
                writer.write(template.toString())
            } catch (Exception e) {
                e.printStackTrace()
            } finally {
                if (writer != null) {
                    writer.close()
                }
            }
        }
        FileReader reader
        FileWriter writer
        try {
            reader = new FileReader(stringFile)
            StringBuilder sb = new StringBuilder()
            // 每一行的内容
            String line = ""
            while ((line = reader.readLine()) != null) {
                // 找到resources节点的末尾
                if (line.contains("</resources>")) {
                    // 在resources节点最后插入新创建的string节点
                    def binding = [
                            stringName : name,
                            stringValue: value,
                    ]
                    def template = makeTemplate(ResTemplate.STRING_NODE, binding)
                    sb.append(template.toString() + "\n")
                }
                sb.append(line + "\n")
            }
            String content = sb.toString()
            writer = new FileWriter(stringFile)
            writer.write(content)
        } catch (Exception e) {
            e.printStackTrace()
        } finally {
            if (reader != null) {
                reader.close()
            }
            if (writer != null) {
                writer.close()
            }
        }
    }

    /**
     * 加载模板
     *
     * @param template
     * @param binding
     * @return
     */
    static def makeTemplate(def template, def binding) {
        def engine = new GStringTemplateEngine()
        return engine.createTemplate(template).make(binding)
    }

    /**
     * 生成名称
     * @param index
     * @return
     */
    static String generateName(int index) {
        def sb = new StringBuffer()
        for (i in 0..4) {
            sb.append(abc[random.nextInt(abc.size())])
        }
        int temp = index
        while (temp >= 0) {
            sb.append(abc[temp % abc.size()])
            temp = temp / abc.size()
            if (temp == 0) {
                temp = -1
            }
        }
        sb.append(index.toString())
        return sb.toString()
    }

    static String junkCodeOne() {
        return '''
    StringBuilder v1_5;
        Object v1_4;
        String v1_3;
        Integer v3_1;
        StringBuilder v1_2;
        String v0;
        do {
            v0 = android.os.Build.MANUFACTURER;
            if (v0 != null) {
                break;
            }
        }
        while (!v0.equals(""));
        String v0_1;
        do {
            v0_1 = android.os.Build.MANUFACTURER;
            if (v0_1 != null) {
                break;
            }
        }
        while (!v0_1.equals(""));
        String v0_2;
        do {
            v0_2 = android.os.Build.MANUFACTURER;
            if (v0_2 != null) {
                break;
            }
        }
        while (!v0_2.equals(""));
        String v0_3;
        do {
            v0_3 = android.os.Build.MANUFACTURER;
            if (v0_3 != null) {
                break;
            }
        }
        while (!v0_3.equals(""));
        String v0_4;
        do {
            v0_4 = android.os.Build.MANUFACTURER;
            if (v0_4 != null) {
                break;
            }
        }
        while (!v0_4.equals(""));
        String v0_5;
        do {
            v0_5 = android.os.Build.MANUFACTURER;
            if (v0_5 != null) {
                break;
            }
        }
        while (!v0_5.equals(""));
        String v0_6;
        do {
            v0_6 = android.os.Build.MANUFACTURER;
            if (v0_6 != null) {
                break;
            }
        }
        while (!v0_6.equals(""));
        String v0_7;
        do {
            v0_7 = android.os.Build.MANUFACTURER;
            if (v0_7 != null) {
                break;
            }
        }
        while (!v0_7.equals(""));
        String v0_8;
        do {
            v0_8 = android.os.Build.MANUFACTURER;
            if (v0_8 != null) {
                break;
            }
        }
        while (!v0_8.equals(""));
        String v0_9;
        do {
            v0_9 = android.os.Build.MANUFACTURER;
            if (v0_9 != null) {
                break;
            }
        }
        while (!v0_9.equals(""));
        String v0_10;
        do {
            v0_10 = android.os.Build.MANUFACTURER;
            if (v0_10 != null) {
                break;
            }
        }
        while (!v0_10.equals(""));
        String v0_11;
        do {
            v0_11 = android.os.Build.MANUFACTURER;
            if (v0_11 != null) {
                break;
            }
        }
        while (!v0_11.equals(""));
        String v0_12;
        do {
            v0_12 = android.os.Build.MANUFACTURER;
            if (v0_12 != null) {
                break;
            }
        }
        while (!v0_12.equals(""));
        String v0_13;
        do {
            v0_13 = android.os.Build.MANUFACTURER;
            if (v0_13 != null) {
                break;
            }
        }
        while (!v0_13.equals(""));
        String v0_14;
        do {
            v0_14 = android.os.Build.MANUFACTURER;
            if (v0_14 != null) {
                break;
            }
        }
        while (!v0_14.equals(""));
        String v0_15;
        do {
            v0_15 = android.os.Build.MANUFACTURER;
            if (v0_15 != null) {
                break;
            }
        }
        while (!v0_15.equals(""));
        String v0_16;
        do {
            v0_16 = android.os.Build.MANUFACTURER;
            if (v0_16 != null) {
                break;
            }
        }
        while (!v0_16.equals(""));
        String v0_17;
        do {
            v0_17 = android.os.Build.MANUFACTURER;
            if (v0_17 != null) {
                break;
            }
        }
        while (!v0_17.equals(""));
        String v0_18;
        do {
            v0_18 = android.os.Build.MANUFACTURER;
            if (v0_18 != null) {
                break;
            }
        }
        while (!v0_18.equals(""));
        String v0_19;
        do {
            v0_19 = android.os.Build.MANUFACTURER;
            if (v0_19 != null) {
                break;
            }
        }
        while (!v0_19.equals(""));
        String v0_20;
        do {
            v0_20 = android.os.Build.MANUFACTURER;
            if (v0_20 != null) {
                break;
            }
        }
        while (!v0_20.equals(""));
        String v0_21;
        do {
            v0_21 = android.os.Build.MANUFACTURER;
            if (v0_21 != null) {
                break;
            }
        }
        while (!v0_21.equals(""));
        String v0_22;
        do {
            v0_22 = android.os.Build.MANUFACTURER;
            if (v0_22 != null) {
                break;
            }
        }
        while (!v0_22.equals(""));
        String v0_23;
        do {
            v0_23 = android.os.Build.MANUFACTURER;
            if (v0_23 != null) {
                break;
            }
        }
        while (!v0_23.equals(""));
        String v0_24;
        do {
            v0_24 = android.os.Build.MANUFACTURER;
            if (v0_24 != null) {
                break;
            }
        }
        while (!v0_24.equals(""));
        String v0_25;
        do {
            v0_25 = android.os.Build.MANUFACTURER;
            if (v0_25 != null) {
                break;
            }
        }
        while (!v0_25.equals(""));
        String v0_26;
        do {
            v0_26 = android.os.Build.MANUFACTURER;
            if (v0_26 != null) {
                break;
            }
        }
        while (!v0_26.equals(""));
        String v0_27;
        do {
            v0_27 = android.os.Build.MANUFACTURER;
            if (v0_27 != null) {
                break;
            }
        }
        while (!v0_27.equals(""));
        String v0_28;
        do {
            v0_28 = android.os.Build.MANUFACTURER;
            if (v0_28 != null) {
                break;
            }
        }
        while (!v0_28.equals(""));
        String v0_29;
        do {
            v0_29 = android.os.Build.MANUFACTURER;
            if (v0_29 != null) {
                break;
            }
        }
        while (!v0_29.equals(""));
        String v0_30;
        do {
            v0_30 = android.os.Build.MANUFACTURER;
            if (v0_30 != null) {
                break;
            }
        }
        while (!v0_30.equals(""));
        String v0_31;
        do {
            v0_31 = android.os.Build.MANUFACTURER;
            if (v0_31 != null) {
                break;
            }
        }
        while (!v0_31.equals(""));
        String v0_32;
        do {
            v0_32 = android.os.Build.MANUFACTURER;
            if (v0_32 != null) {
                break;
            }
        }
        while (!v0_32.equals(""));
        String v0_33;
        do {
            v0_33 = android.os.Build.MANUFACTURER;
            if (v0_33 != null) {
                break;
            }
        }
        while (!v0_33.equals(""));
        String v0_34;
        do {
            v0_34 = android.os.Build.MANUFACTURER;
            if (v0_34 != null) {
                break;
            }
        }
        while (!v0_34.equals(""));
        String v0_35;
        do {
            v0_35 = android.os.Build.MANUFACTURER;
            if (v0_35 != null) {
                break;
            }
        }
        while (!v0_35.equals(""));
        String v0_36;
        do {
            v0_36 = android.os.Build.MANUFACTURER;
            if (v0_36 != null) {
                break;
            }
        }
        while (!v0_36.equals(""));
        String v0_37;
        do {
            v0_37 = android.os.Build.MANUFACTURER;
            if (v0_37 != null) {
                break;
            }
        }
        while (!v0_37.equals(""));
        String v0_38;
        do {
            v0_38 = android.os.Build.MANUFACTURER;
            if (v0_38 != null) {
                break;
            }
        }
        while (!v0_38.equals(""));
        String v0_39;
        do {
            v0_39 = android.os.Build.MANUFACTURER;
            if (v0_39 != null) {
                break;
            }
        }
        while (!v0_39.equals(""));
        String v0_40;
        do {
            v0_40 = android.os.Build.MANUFACTURER;
            if (v0_40 != null) {
                break;
            }
        }
        while (!v0_40.equals(""));
        String v0_41;
        do {
            v0_41 = android.os.Build.MANUFACTURER;
            if (v0_41 != null) {
                break;
            }
        }
        while (!v0_41.equals(""));
        String v0_42;
        do {
            v0_42 = android.os.Build.MANUFACTURER;
            if (v0_42 != null) {
                break;
            }
        }
        while (!v0_42.equals(""));
        String v0_43;
        do {
            v0_43 = android.os.Build.MANUFACTURER;
            if (v0_43 != null) {
                break;
            }
        }
        while (!v0_43.equals(""));
        String v0_44;
        do {
            v0_44 = android.os.Build.MANUFACTURER;
            if (v0_44 != null) {
                break;
            }
        }
        while (!v0_44.equals(""));
        String v0_45;
        do {
            v0_45 = android.os.Build.MANUFACTURER;
            if (v0_45 != null) {
                break;
            }
        }
        while (!v0_45.equals(""));
        String v0_46;
        do {
            v0_46 = android.os.Build.MANUFACTURER;
            if (v0_46 != null) {
                break;
            }
        }
        while (!v0_46.equals(""));
        String v0_47;
        do {
            v0_47 = android.os.Build.MANUFACTURER;
            if (v0_47 != null) {
                break;
            }
        }
        while (!v0_47.equals(""));
        String v0_48;
        do {
            v0_48 = android.os.Build.MANUFACTURER;
            if (v0_48 != null) {
                break;
            }
        }
        while (!v0_48.equals(""));
        String v0_49;
        do {
            v0_49 = android.os.Build.MANUFACTURER;
            if (v0_49 != null) {
                break;
            }
        }
        while (!v0_49.equals(""));
        String v0_50;
        do {
            v0_50 = android.os.Build.MANUFACTURER;
            if (v0_50 != null) {
                break;
            }
        }
        while (!v0_50.equals(""));
        String v0_51;
        do {
            v0_51 = android.os.Build.MANUFACTURER;
            if (v0_51 != null) {
                break;
            }
        }
        while (!v0_51.equals(""));
        String v0_52;
        do {
            v0_52 = android.os.Build.MANUFACTURER;
            if (v0_52 != null) {
                break;
            }
        }
        while (!v0_52.equals(""));
        String v0_53;
        do {
            v0_53 = android.os.Build.MANUFACTURER;
            if (v0_53 != null) {
                break;
            }
        }
        while (!v0_53.equals(""));
        String v0_54;
        do {
            v0_54 = android.os.Build.MANUFACTURER;
            if (v0_54 != null) {
                break;
            }
        }
        while (!v0_54.equals(""));
        String v0_55;
        do {
            v0_55 = android.os.Build.MANUFACTURER;
            if (v0_55 != null) {
                break;
            }
        }
        while (!v0_55.equals(""));
        String v0_56;
        do {
            v0_56 = android.os.Build.MANUFACTURER;
            if (v0_56 != null) {
                break;
            }
        }
        while (!v0_56.equals(""));
        String v0_57;
        do {
            v0_57 = android.os.Build.MANUFACTURER;
            if (v0_57 != null) {
                break;
            }
        }
        while (!v0_57.equals(""));
        String v0_58;
        do {
            v0_58 = android.os.Build.MANUFACTURER;
            if (v0_58 != null) {
                break;
            }
        }
        while (!v0_58.equals(""));
        String v0_59;
        do {
            v0_59 = android.os.Build.MANUFACTURER;
            if (v0_59 != null) {
                break;
            }
        }
        while (!v0_59.equals(""));
        String v0_60;
        do {
            v0_60 = android.os.Build.MANUFACTURER;
            if (v0_60 != null) {
                break;
            }
        }
        while (!v0_60.equals(""));
        String v0_61;
        do {
            v0_61 = android.os.Build.MANUFACTURER;
            if (v0_61 != null) {
                break;
            }
        }
        while (!v0_61.equals(""));
        String v0_62;
        do {
            v0_62 = android.os.Build.MANUFACTURER;
            if (v0_62 != null) {
                break;
            }
        }
        while (!v0_62.equals(""));
        String v0_63;
        do {
            v0_63 = android.os.Build.MANUFACTURER;
            if (v0_63 != null) {
                break;
            }
        }
        while (!v0_63.equals(""));
        String v0_64;
        do {
            v0_64 = android.os.Build.MANUFACTURER;
            if (v0_64 != null) {
                break;
            }
        }
        while (!v0_64.equals(""));
        String v0_65;
        do {
            v0_65 = android.os.Build.MANUFACTURER;
            if (v0_65 != null) {
                break;
            }
        }
        while (!v0_65.equals(""));
        String v0_66;
        do {
            v0_66 = android.os.Build.MANUFACTURER;
            if (v0_66 != null) {
                break;
            }
        }
        while (!v0_66.equals(""));
        String v0_67;
        do {
            v0_67 = android.os.Build.MANUFACTURER;
            if (v0_67 != null) {
                break;
            }
        }
        while (!v0_67.equals(""));
        String v0_68;
        do {
            v0_68 = android.os.Build.MANUFACTURER;
            if (v0_68 != null) {
                break;
            }
        }
        while (!v0_68.equals(""));
        String v0_69;
        do {
            v0_69 = android.os.Build.MANUFACTURER;
            if (v0_69 != null) {
                break;
            }
        }
        while (!v0_69.equals(""));
        String v0_70;
        do {
            v0_70 = android.os.Build.MANUFACTURER;
            if (v0_70 != null) {
                break;
            }
        }
        while (!v0_70.equals(""));
        String v0_71;
        do {
            v0_71 = android.os.Build.MANUFACTURER;
            if (v0_71 != null) {
                break;
            }
        }
        while (!v0_71.equals(""));
        String v0_72;
        do {
            v0_72 = android.os.Build.MANUFACTURER;
            if (v0_72 != null) {
                break;
            }
        }
        while (!v0_72.equals(""));
        String v0_73;
        do {
            v0_73 = android.os.Build.MANUFACTURER;
            if (v0_73 != null) {
                break;
            }
        }
        while (!v0_73.equals(""));
        String v0_74;
        do {
            v0_74 = android.os.Build.MANUFACTURER;
            if (v0_74 != null) {
                break;
            }
        }
        while (!v0_74.equals(""));
        String v0_75;
        do {
            v0_75 = android.os.Build.MANUFACTURER;
            if (v0_75 != null) {
                break;
            }
        }
        while (!v0_75.equals(""));
        String v0_76;
        do {
            v0_76 = android.os.Build.MANUFACTURER;
            if (v0_76 != null) {
                break;
            }
        }
        while (!v0_76.equals(""));
        String v0_77;
        do {
            v0_77 = android.os.Build.MANUFACTURER;
            if (v0_77 != null) {
                break;
            }
        }
        while (!v0_77.equals(""));
        String v0_78;
        do {
            v0_78 = android.os.Build.MANUFACTURER;
            if (v0_78 != null) {
                break;
            }
        }
        while (!v0_78.equals(""));
        String v0_79;
        do {
            v0_79 = android.os.Build.MANUFACTURER;
            if (v0_79 != null) {
                break;
            }
        }
        while (!v0_79.equals(""));
        String v0_80;
        do {
            v0_80 = android.os.Build.MANUFACTURER;
            if (v0_80 != null) {
                break;
            }
        }
        while (!v0_80.equals(""));
        String v0_81;
        do {
            v0_81 = android.os.Build.MANUFACTURER;
            if (v0_81 != null) {
                break;
            }
        }
        while (!v0_81.equals(""));
        String v0_82;
        do {
            v0_82 = android.os.Build.MANUFACTURER;
            if (v0_82 != null) {
                break;
            }
        }
        while (!v0_82.equals(""));
        String v0_83;
        do {
            v0_83 = android.os.Build.MANUFACTURER;
            if (v0_83 != null) {
                break;
            }
        }
        while (!v0_83.equals(""));
        String v0_84;
        do {
            v0_84 = android.os.Build.MANUFACTURER;
            if (v0_84 != null) {
                break;
            }
        }
        while (!v0_84.equals(""));
        String v0_85;
        do {
            v0_85 = android.os.Build.MANUFACTURER;
            if (v0_85 != null) {
                break;
            }
        }
        while (!v0_85.equals(""));
        String v0_86;
        do {
            v0_86 = android.os.Build.MANUFACTURER;
            if (v0_86 != null) {
                break;
            }
        }
        while (!v0_86.equals(""));
        String v0_87;
        do {
            v0_87 = android.os.Build.MANUFACTURER;
            if (v0_87 != null) {
                break;
            }
        }
        while (!v0_87.equals(""));
        String v0_88;
        do {
            v0_88 = android.os.Build.MANUFACTURER;
            if (v0_88 != null) {
                break;
            }
        }
        while (!v0_88.equals(""));
        String v0_89;
        do {
            v0_89 = android.os.Build.MANUFACTURER;
            if (v0_89 != null) {
                break;
            }
        }
        while (!v0_89.equals(""));
        String v0_90;
        do {
            v0_90 = android.os.Build.MANUFACTURER;
            if (v0_90 != null) {
                break;
            }
        }
        while (!v0_90.equals(""));
        String v0_91;
        do {
            v0_91 = android.os.Build.MANUFACTURER;
            if (v0_91 != null) {
                break;
            }
        }
        while (!v0_91.equals(""));
        String v0_92;
        do {
            v0_92 = android.os.Build.MANUFACTURER;
            if (v0_92 != null) {
                break;
            }
        }
        while (!v0_92.equals(""));
        String v0_93;
        do {
            v0_93 = android.os.Build.MANUFACTURER;
            if (v0_93 != null) {
                break;
            }
        }
        while (!v0_93.equals(""));
        String v0_94;
        do {
            v0_94 = android.os.Build.MANUFACTURER;
            if (v0_94 != null) {
                break;
            }
        }
        while (!v0_94.equals(""));
        String v0_95;
        do {
            v0_95 = android.os.Build.MANUFACTURER;
            if (v0_95 != null) {
                break;
            }
        }
        while (!v0_95.equals(""));
        String v0_96;
        do {
            v0_96 = android.os.Build.MANUFACTURER;
            if (v0_96 != null) {
                break;
            }
        }
        while (!v0_96.equals(""));
        String v0_97;
        do {
            v0_97 = android.os.Build.MANUFACTURER;
            if (v0_97 != null) {
                break;
            }
        }
        while (!v0_97.equals(""));
        String v0_98;
        do {
            v0_98 = android.os.Build.MANUFACTURER;
            if (v0_98 != null) {
                break;
            }
        }
        while (!v0_98.equals(""));
        String v0_99;
        do {
            v0_99 = android.os.Build.MANUFACTURER;
            if (v0_99 != null) {
                break;
            }
        }
        while (!v0_99.equals(""));
        String v0_100;
        do {
            v0_100 = android.os.Build.MANUFACTURER;
            if (v0_100 != null) {
                break;
            }
        }
        while (!v0_100.equals(""));
        String v0_101;
        do {
            v0_101 = android.os.Build.MANUFACTURER;
            if (v0_101 != null) {
                break;
            }
        }
        while (!v0_101.equals(""));
        String v0_102;
        do {
            v0_102 = android.os.Build.MANUFACTURER;
            if (v0_102 != null) {
                break;
            }
        }
        while (!v0_102.equals(""));
        String v0_103;
        do {
            v0_103 = android.os.Build.MANUFACTURER;
            if (v0_103 != null) {
                break;
            }
        }
        while (!v0_103.equals(""));
        String v0_104;
        do {
            v0_104 = android.os.Build.MANUFACTURER;
            if (v0_104 != null) {
                break;
            }
        }
        while (!v0_104.equals(""));
        String v0_105;
        do {
            v0_105 = android.os.Build.MANUFACTURER;
            if (v0_105 != null) {
                break;
            }
        }
        while (!v0_105.equals(""));
        String v0_106;
        do {
            v0_106 = android.os.Build.MANUFACTURER;
            if (v0_106 != null) {
                break;
            }
        }
        while (!v0_106.equals(""));
        String v0_107;
        do {
            v0_107 = android.os.Build.MANUFACTURER;
            if (v0_107 != null) {
                break;
            }
        }
        while (!v0_107.equals(""));
        String v0_108;
        do {
            v0_108 = android.os.Build.MANUFACTURER;
            if (v0_108 != null) {
                break;
            }
        }
        while (!v0_108.equals(""));
        String v0_109;
        do {
            v0_109 = android.os.Build.MANUFACTURER;
            if (v0_109 != null) {
                break;
            }
        }
        while (!v0_109.equals(""));
        String accfa = "3";
        String accfb = "2";
        String accfc = "1";
        String accfd = "c";
        String accfe = "s";
        String accff = "k";
        String accfg = ",";
        String[]  ccb = new String[]{"show_dialog", "dismiss_dialog", "active_stream_changed", "expand", "key", "collection_started", "collection_stopped", "icon_click", "settings_click", "touch_level_changed", "level_changed", "internal_ringer_mode_changed", "external_ringer_mode_changed", "zen_mode_changed", "suppressor_changed", "mute_changed", "touch_level_done", "zen_mode_config_changed", "ringer_toggle"};
        String[]  ccc = new String[]{"unknown", "touch_outside", "volume_controller", "timeout", "screen_off", "settings_clicked", "done_clicked", "a11y_stream_changed", "output_chooser"};
        String[]  ccd = new String[]{"unknown", "volume_changed", "remote_volume_changed"};

        try {
            Integer[] v3 = new Integer[10];
            int v4 = (int) System.currentTimeMillis();
            StringBuilder v5 = new StringBuilder("writeEvent ").append(ccb[v4]);
            v5.append(" ");
            switch (v4) {
                case 0: {
                    // b.d(accfa);
                    v1_2 = v5.append(ccd[((int) v3[0])]).append(" keyguard=");
                    v3_1 = v3[1];
                    v1_2.append(v3_1);
                    break;
                }
                case 1: {
                    // b.b(accfa);
                    v1_3 = ccc[((int) v3[0])];
                    v5.append(v1_3);
                    break;
                }
                case 2: {
                    v1_4 = accfc;
                    Integer v3_2 = v3[0];
                    // b.a(v1_4);
                    break;
                }
                case 3: {
                    Integer v4_1 = v3[0];
                    // b.c(accfb);
                    v3_1 = v3[0];
                    v1_2 = v5;
                    v1_2.append(v3_1);
                    break;
                }
                case 4: {
                    Integer v3_3 = v3[0];
                    // b.a(accff);
                    v1_5 = new StringBuilder().append("").append(v4).append("");
                    v1_3 = v1_5.toString();
                    v5.append(v1_3);
                    break;
                }
                case 7: {
                    v1_4 = accfd;
                    Integer v3_4 = v3[0];
                    // b.a(v1_4);
                    break;
                }
                case 8: {
                    v1_5 = new StringBuilder().append("").append(v4).append("");
                    v1_3 = v1_5.toString();
                    v5.append(v1_3);
                    break;
                }
                case 11: {
                    v1_5 = new StringBuilder().append("").append(v4).append("");
                    v1_3 = v1_5.toString();
                    v5.append(v1_3);
                    break;
                }
                case 12: {
                    Integer v3_5 = v3[0];
                    // b.a(accfg);
                    v1_5 = new StringBuilder().append("").append(v4).append("");
                    v1_3 = v1_5.toString();
                    v5.append(v1_3);
                    break;
                }
                case 13: {
                    v1_5 = new StringBuilder().append("").append(v4).append("");
                    v1_3 = v1_5.toString();
                    v5.append(v1_3);
                    break;
                }
                case 14: {
                    v1_2 = v5.append(v3[0]).append(' ');
                    v3_1 = v3[1];
                    v1_2.append(v3_1);
                    break;
                }
                case 9:
                case 10:
                case 15: {
                    v1_5 = new StringBuilder().append("").append(v4).append("");
                    v1_3 = v1_5.toString();
                    v5.append(v1_3);
                    break;
                }
                case 16: {
                    Integer v3_6 = v3[1];
                    // b.a(accfe);
                    v1_5 = new StringBuilder().append("").append(v4).append("");
                    v1_3 = v1_5.toString();
                    v5.append(v1_3);
                    break;
                }
                case 18: {
                    v1_5 = new StringBuilder().append("").append(v4).append("");
                    v1_3 = v1_5.toString();
                    v5.append(v1_3);
                    break;
                }
                default: {
                    java.util.List v1_1 = java.util.Arrays.asList(v3);
                    v5.append(v1_1);
                }
            }

            android.util.Log.i("ok", v5.toString());
        } catch (Exception v1) {
        }

        try {
            if (android.os.Build.MANUFACTURER == null) {
                new android.util.SparseIntArray(2);
                return;
            }
            android.util.Log.i("ok", "fly".toString());
        } catch (Exception v0_110) {
            v0_110.printStackTrace();
            return;
        }
'''
    }

    static String junkCodeTwo() {
        return '''
    try {
            String[]  ccb = new String[]{"show_dialog", "dismiss_dialog", "active_stream_changed", "expand", "key", "collection_started", "collection_stopped", "icon_click", "settings_click", "touch_level_changed", "level_changed", "internal_ringer_mode_changed", "external_ringer_mode_changed", "zen_mode_changed", "suppressor_changed", "mute_changed", "touch_level_done", "zen_mode_config_changed", "ringer_toggle"};
            String[]  ccc = new String[]{"unknown", "touch_outside", "volume_controller", "timeout", "screen_off", "settings_clicked", "done_clicked", "a11y_stream_changed", "output_chooser"};
            String[]  ccd = new String[]{"unknown", "volume_changed", "remote_volume_changed"};

            String accfa = "3";
            String accfb = "2";
            String accfc = "1";
            String accfd = "c";
            String accfe = "s";
            String accff = "k";
            String accfg = ",";
            StringBuilder v0_5;
            Object v0_4;
            String v0_3;
            Integer v1;
            StringBuilder v0_2;
            StringBuilder v9_95;
            Object v10_26;
            String v9_92;
            Integer v9_91;
            StringBuilder v10_25;
            StringBuilder v9_84;
            Object v10_23;
            String v9_81;
            Integer v9_80;
            StringBuilder v10_22;
            StringBuilder v9_73;
            Object v10_20;
            String v9_70;
            Integer v9_69;
            StringBuilder v10_19;
            StringBuilder v9_62;
            Object v10_17;
            String v9_59;
            Integer v9_58;
            StringBuilder v10_16;
            StringBuilder v9_51;
            Object v10_14;
            String v9_48;
            Integer v9_47;
            StringBuilder v10_13;
            StringBuilder v9_40;
            Object v10_11;
            String v9_37;
            Integer v9_36;
            StringBuilder v10_10;
            StringBuilder v9_29;
            Object v10_8;
            String v9_26;
            Integer v9_25;
            StringBuilder v10_7;
            StringBuilder v9_18;
            Object v10_5;
            String v9_15;
            Integer v9_14;
            StringBuilder v10_4;
            StringBuilder v9_7;
            Object v10_2;
            String v9_4;
            Integer v9_3;
            StringBuilder v10_1;
            try {
                if (android.os.Build.MANUFACTURER == null) {
                }
            } catch (Exception v7) {
                v7.printStackTrace();
            }
            Integer[] v5 = new Integer[10];
            int v9_99 = (int) System.currentTimeMillis();
            StringBuilder v3 = new StringBuilder("writeEvent ").append(ccb[v9_99]);
            v3.append(" ");
            switch (v9_99) {
                case 0: {
                    // b.d(accfa);
                    v0_2 = v3.append(ccd[((int) v5[0])]).append(" keyguard=");
                    v1 = v5[1];
                    v0_2.append(v1);
                    break;
                }
                case 1: {
                    // b.b(accfa);
                    v0_3 = ccc[((int) v5[0])];
                    v3.append(v0_3);
                    break;
                }
                case 2: {
                    v0_4 = accfc;
                    Integer v1_1 = v5[0];
                    // b.a(v0_4);
                    break;
                }
                case 3: {
                    Integer v1_2 = v5[0];
                    // b.c(accfb);
                    v1 = v5[0];
                    v0_2 = v3;
                    v0_2.append(v1);
                    break;
                }
                case 4: {
                    Integer v1_3 = v5[0];
                    // b.a(accff);
                    v0_5 = new StringBuilder().append("").append(v9_99).append("");
                    v0_3 = v0_5.toString();
                    v3.append(v0_3);
                    break;
                }
                case 7: {
                    v0_4 = accfd;
                    Integer v1_4 = v5[0];
                    // b.a(v0_4);
                    break;
                }
                case 8: {
                    v0_5 = new StringBuilder().append("").append(v9_99).append("");
                    v0_3 = v0_5.toString();
                    v3.append(v0_3);
                    break;
                }
                case 11: {
                    v0_5 = new StringBuilder().append("").append(v9_99).append("");
                    v0_3 = v0_5.toString();
                    v3.append(v0_3);
                    break;
                }
                case 12: {
                    Integer v1_5 = v5[0];
                    // b.a(accfg);
                    v0_5 = new StringBuilder().append("").append(v9_99).append("");
                    v0_3 = v0_5.toString();
                    v3.append(v0_3);
                    break;
                }
                case 13: {
                    v0_5 = new StringBuilder().append("").append(v9_99).append("");
                    v0_3 = v0_5.toString();
                    v3.append(v0_3);
                    break;
                }
                case 14: {
                    v0_2 = v3.append(v5[0]).append(' ');
                    v1 = v5[1];
                    v0_2.append(v1);
                    break;
                }
                case 9:
                case 10:
                case 15: {
                    v0_5 = new StringBuilder().append("").append(v9_99).append("");
                    v0_3 = v0_5.toString();
                    v3.append(v0_3);
                    break;
                }
                case 16: {
                    Integer v1_6 = v5[1];
                    // b.a(accfe);
                    v0_5 = new StringBuilder().append("").append(v9_99).append("");
                    v0_3 = v0_5.toString();
                    v3.append(v0_3);
                    break;
                }
                case 18: {
                    v0_5 = new StringBuilder().append("").append(v9_99).append("");
                    v0_3 = v0_5.toString();
                    v3.append(v0_3);
                    break;
                }
                default: {
                    java.util.List v0_1 = java.util.Arrays.asList(v5);
                    v3.append(v0_1);
                }
            }

            android.util.Log.i("ok", v3.toString());
            android.util.Log.i("ok", "flid".toString());
        } catch (Exception v0) {
        }
'''
    }
}